package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.models.AllDictionariesWarp
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.Doctor
import com.compass.mobile.models.HomeMenuWarp
import com.compass.mobile.models.HospitalBean
import com.compass.mobile.models.PatientBean
import com.compass.mobile.models.UpdateInfo
import com.compass.mobile.models.User
import com.compass.mobile.models.YDUser
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description：
 * Date:2024/5/29 8:56
 */
class MainViewModel : BaseViewModel() {
    val loginLiveData = MutableLiveData<User?>()
    val userLiveData = MutableLiveData<YDUser?>()
    val patientDefLiveData = MutableLiveData<PatientBean?>()
    val hospitalListLiveData = MutableLiveData<MutableList<HospitalBean>?>()
    val allDictionariesLiveData = MutableLiveData<AllDictionariesWarp?>()
    val doctorsLiveData = MutableLiveData<MutableList<Doctor>?>()
    val homePageWarp = MutableLiveData<HomeMenuWarp>()
    val versionLiveData = MutableLiveData<UpdateInfo?>()
    val agreementLiveData = MutableLiveData<String?>()

    fun getAppVersion() {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.getAppVersion("3")
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
            }
        }, successBlock = {
            versionLiveData.value = it
        })
    }
    /**
     * 获取全部字典缓存
     */
    fun getAllDictionaries() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getDictionaries()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            allDictionariesLiveData.value = it

        })
    }


    /**
     * 获取全部字典缓存
     * paramsKey  tips.consultation.notice//护理   tips.consultation.buy//问诊
     * content.privacyAgreement//隐私   content.serviceAgreement服务协议
     */
    fun getHospitalConfig(paramsKey:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHospitalConfig(paramsKey)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            agreementLiveData.value = it

        })
    }

    fun getUserPhoneForToken(token: String?) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.geUserPhoneForToken(token)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                userLiveData.value=null
            }
        }, successBlock = {
            userLiveData.value=it
        })
    }

    fun boxLogin(loginCode: String?) {
        val params = JsonObject().apply {
            addProperty("loginCode", loginCode)
            addProperty("socialType", "37")
        }

        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.boxLogin(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            loginLiveData.value = it
        })
    }


    fun getHospitalList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getHospitalList()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            hospitalListLiveData.value = it
        })
    }


    fun getDefPatient() {
        launchUIWithResult(responseBlock = {

            ApiManager.api.getDefPatient()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                patientDefLiveData.value=null
            }
        }, successBlock = {
            patientDefLiveData.value=it
        })
    }


    fun getMenuConfig() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getMenuConfig()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if(it != null){
                homePageWarp.value  = it
            }
        })
    }



    fun geRecommendDoctor(pageNo: Int=1, pageSize: Int = 3) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geRecommendDoctor(pageNo,pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                doctorsLiveData.value = null
            } else {
                doctorsLiveData.value = it.list
            }
        })
    }



}